﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;

namespace EnterpriseFund.Controllers
{
    public class TaxDataCompareController : Controller
    {
        //
        // GET: /TaxDataCompare/

        public ActionResult Index(string FTableName)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("FTableName");
            dt.Columns.Add("SYFTableName");
            dt.Columns.Add("Columns");
            dt.Columns.Add("SYColumns");
            dt.Columns.Add("KeyColumns");
            dt.Columns.Add("SYKeyColumns");
            dt.Columns.Add("ColumnNames");
            dt.TableName = "DataCompare";
            dt.ReadXml(Server.MapPath("~") + "\\DataCompare.xml");

            List<DataCompareComB> combList = new List<DataCompareComB>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                combList.Add(new DataCompareComB(dt.Rows[i]["FTableName"].ToString(), dt.Rows[i]["Name"].ToString()));
            }

            ViewData["combList"] = combList;
            ViewData["FTableName"] = FTableName;
            ViewData["KeyColumns"] = "";
            DataSet newds = new DataSet();
            if (FTableName != "")
            {
                var q = from nm in dt.AsEnumerable() where nm.Field<string>("FTableName") == FTableName select nm;
                BLL.Common commonBLL = new BLL.Common();
                foreach (var v in q)
                {
                    string[] columns = v["Columns"].ToString().Split(',');
                    string[] SYColumns = v["SYColumns"].ToString().Split(',');
                    string[] ColumnNames = v["ColumnNames"].ToString().Split(',');
                    string nc = "";
                    DataTable showDt = new DataTable();
                    for (int i = 0; i < columns.Count(); i++)
                    {
                        showDt.Columns.Add(columns[i]);
                        showDt.Columns[columns[i]].Caption = ColumnNames[i];
                        if (nc == "")
                        {
                            nc += columns[i] + " as " + SYColumns[i];
                        }
                        else
                        {
                            nc += "," + columns[i] + " as " + SYColumns[i];
                        }
                    }

                    ViewData["showDt"] = showDt;
                    ViewData["KeyColumns"] = v["KeyColumns"].ToString();
                    DataSet ds = commonBLL.GetCompareDataBySQL(v["FTableName"].ToString(), nc);
                    DataSet syds = commonBLL.GetCompareDataByOra(v["SYFTableName"].ToString(), nc);
                    newds = GetDataTable(ds.Tables[0], syds.Tables[0], v["KeyColumns"].ToString());
                }
            }
            return View("Index", newds);
        }

        public ActionResult SYDataPartial()
        {
            return PartialView();
        }

        public ActionResult DataPartial()
        {
            return PartialView();
        }

        public ActionResult TaxDataCompareSave()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("FTableName");
            dt.Columns.Add("SYFTableName");
            dt.Columns.Add("Columns");
            dt.Columns.Add("SYColumns");
            dt.Columns.Add("KeyColumns");
            dt.Columns.Add("SYKeyColumns");
            dt.Columns.Add("ColumnNames");
            dt.TableName = "DataCompare";
            dt.ReadXml(Server.MapPath("~") + "\\DataCompare.xml");
            DataSet newds = new DataSet();
            string FTableName = DevExpress.Web.Mvc.ComboBoxExtension.GetValue<string>("FTableName");
            if (FTableName != "")
            {
                var q = from nm in dt.AsEnumerable() where nm.Field<string>("FTableName") == FTableName select nm;
                BLL.Common commonBLL = new BLL.Common();
                foreach (var v in q)
                {
                    string[] columns = v["Columns"].ToString().Split(',');
                    string[] SYColumns = v["SYColumns"].ToString().Split(',');
                    string[] ColumnNames = v["ColumnNames"].ToString().Split(',');
                    string nc = "";
                    DataTable showDt = new DataTable();
                    for (int i = 0; i < columns.Count(); i++)
                    {
                        showDt.Columns.Add(columns[i]);
                        showDt.Columns[columns[i]].Caption = ColumnNames[i];
                        if (nc == "")
                        {
                            nc += columns[i] + " as " + SYColumns[i];
                        }
                        else
                        {
                            nc += "," + columns[i] + " as " + SYColumns[i];
                        }
                    }

                    ViewData["showDt"] = showDt;
                    ViewData["KeyColumns"] = v["KeyColumns"].ToString();
                    DataSet ds = commonBLL.GetCompareDataBySQL(v["FTableName"].ToString(), nc);
                    DataSet syds = commonBLL.GetCompareDataByOra(v["SYFTableName"].ToString(), nc);
                    newds = GetDataTable(ds.Tables[0], syds.Tables[0], v["KeyColumns"].ToString());

                    if (newds.Tables[1].Rows.Count > 0)
                    {
                        DataRow[] Adddr = newds.Tables[1].Select("type='syAdd'");
                        DataRow[] Editdr = newds.Tables[1].Select("type='Update'");
                        commonBLL.UpdateCompareData(v["KeyColumns"].ToString(), FTableName, v["Columns"].ToString(), Adddr, Editdr);
                    }
                }
            }

            return Index(FTableName);
        }

        public DataSet GetDataTable(DataTable dt, DataTable syDt, string key)
        {
            DataTable newDt = new DataTable();
            DataTable synewDt = new DataTable();
            foreach (DataColumn dc in dt.Columns)
            {
                newDt.Columns.Add(dc.ColumnName);
                synewDt.Columns.Add(dc.ColumnName);
            }
            newDt.Columns.Add("type");
            synewDt.Columns.Add("type");

            for (int i = 0; i < syDt.Rows.Count; i++)
            {
                var q = from nm in dt.AsEnumerable() where nm.Field<string>(key) == syDt.Rows[i][key].ToString() select nm;
                if (q.Count() > 0)
                {
                    foreach (var v in q)
                    {
                        foreach (DataColumn dc in dt.Columns)
                        {
                            if (v[dc.ColumnName].ToString() != syDt.Rows[i][dc.ColumnName].ToString())
                            {
                                DataRow ndr = newDt.NewRow();
                                foreach (DataColumn sdc in dt.Columns)
                                {
                                    ndr[sdc.ColumnName] = v[sdc.ColumnName];
                                }
                                ndr["type"] = "Update";
                                newDt.Rows.Add(ndr);

                                DataRow syndr = synewDt.NewRow();
                                foreach (DataColumn sdc in dt.Columns)
                                {
                                    syndr[sdc.ColumnName] = syDt.Rows[i][sdc.ColumnName];
                                }
                                syndr["type"] = "Update";
                                synewDt.Rows.Add(syndr);
                                break;
                            }
                        }

                        //DataRow ndr2 = newDt.NewRow();
                        //foreach (DataColumn sdc in dt.Columns)
                        //{
                        //    ndr2[sdc.ColumnName] = v[sdc.ColumnName];
                        //}
                        //ndr2["type"] = "no";
                        //newDt.Rows.Add(ndr2);

                        //DataRow syndr2 = syDt.NewRow();
                        //foreach (DataColumn sdc in dt.Columns)
                        //{
                        //    syndr2[sdc.ColumnName] = syDt.Rows[i][sdc.ColumnName];
                        //}
                        //syndr2["type"] = "no";
                        //synewDt.Rows.Add(syndr2);
                        //break;
                    }
                }
                else
                {
                    DataRow ndr = newDt.NewRow();
                    ndr["type"] = "syAdd";
                    newDt.Rows.Add(ndr);
                    DataRow syndr = synewDt.NewRow();
                    foreach (DataColumn sdc in dt.Columns)
                    {
                        syndr[sdc.ColumnName] = syDt.Rows[i][sdc.ColumnName];
                    }
                    syndr["type"] = "syAdd";
                    synewDt.Rows.Add(syndr);
                }
            }


            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var q = from nm in syDt.AsEnumerable() where nm.Field<string>(key) == dt.Rows[i][key].ToString() select nm;
                if (q.Count() > 0)
                {
                   
                }
                else
                {
                    DataRow ndr = newDt.NewRow();
                    foreach (DataColumn sdc in dt.Columns)
                    {
                        ndr[sdc.ColumnName] = dt.Rows[i][sdc.ColumnName];
                    }
                    ndr["type"] = "Add";
                    newDt.Rows.Add(ndr);
                    DataRow syndr = synewDt.NewRow();
                    syndr["type"] = "Add";
                    synewDt.Rows.Add(syndr);
                }
            }

            DataSet ds = new DataSet();
            ds.Tables.Add(newDt);
            ds.Tables.Add(synewDt);
            return ds;
        }

    }

    class DataCompareComB
    {
        public DataCompareComB()
        {
 
        }

        public DataCompareComB(string fTableName, string name)
        {
            FTableName = fTableName;
            Name = name;
        }

        public string FTableName
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }
    }
}
